package com.zzyl.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.RoleDto;
import com.zzyl.service.RoleService;
import com.zzyl.vo.RoleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/role")
@Api(tags = "用户管理")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @PostMapping("/page/{pageNum}/{pageSize}")
    @ApiOperation("角色分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleDto",value = "角色DTO对象",required =true,dataType = "RoleDTO"),
            @ApiImplicitParam(paramType = "path",name = "pageNum",value = "页码",example = "1",dataType = "Integer"),
            @ApiImplicitParam(paramType = "path",name = "pageSize",value = "每页条数",example = "10",dataType = "Integer")
})@ApiOperationSupport(includeParameters = {"roleDto.roleName"})
    public ResponseResult page (@PathVariable("pageNum") Integer pageNum, @PathVariable("pageSize") Integer pageSize, @RequestBody RoleDto roleDto){
        PageResponse response = roleService.page(pageNum,pageSize,roleDto);
        return ResponseResult.success(response);
    }

    @PutMapping
    @ApiOperation("角色新增")
    @ApiImplicitParam(name = "roleDto",value = "角色DTO对象",required =true,dataType = "RoleDTO")
    @ApiOperationSupport(includeParameters ={"roleDto.roleName","roleDto.dataState"})
    public ResponseResult insert(@RequestBody RoleDto roleDto){
        roleService.insert(roleDto);
        return ResponseResult.success();
    }

    @GetMapping("/find-checked-resources/{roleId}")
    @ApiOperation("根据角色查询选中的数据")
    public ResponseResult findCheckedResources(@PathVariable Long roleId){
        List<String> list = roleService.findCheckedResources(roleId);
        return ResponseResult.success(list);
    }

    @PatchMapping
    @ApiOperation("角色修改")
    @ApiImplicitParam(name = "roleDto",value = "角色DTO对象",required = true,dataType = "roleDto")
    @ApiOperationSupport(includeParameters = {"roleDto.roleName","roleDto.dataState","roleDto.dataScope","roleDto.checkedResourceNos","roleDto.checkedDeptNos","roleDto.id"})
    public ResponseResult update(@RequestBody RoleDto roleDto){
        Boolean flag = roleService.update(roleDto);
        return ResponseResult.success();
    }

    @DeleteMapping("/{roleIds}")
    @ApiOperation("删除角色")
    public ResponseResult dalete(@PathVariable("roleIds") Long roleId){
        roleService.delete(roleId);
        return ResponseResult.success();
    }

    @PostMapping("/init-roles")
    @ApiOperation("角色下拉框")
    public ResponseResult getAllRoel(){
        List<RoleVo> list = roleService.getAllRoel();
        return ResponseResult.success(list);
    }
}
